﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BitPortal.Models;
using BitPortal.Business;
using Ext.Net;

namespace BitPortal.Admin.Config.UserConfig.Module
{
    public partial class UserEditConfig : Controls.BaseAdminControl
    {
        public Ext.Net.Window WindowContainer
        {
            get { return this.Window1; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void BindData(string configName)
        {
            if (configName != null)
            {
                PortalConfig config = PortalConfigBLL.GetByName(configName);
                if (config != null)
                {
                    txtName.Text = config.Name;
                    Window1.Title = config.Description;

                    txtDatatype.Text = RichEditor1.DataType = config.DataType;
                    RichEditor1.Value = config.GetValue().ToString();

                    btnUpdate.Hidden = !config.AllowEdit;
                    btnDelete.Hidden = !config.AllowDelete;

                    Window1.Show();
                }
            }
        }

        protected void btnUpdate_Click(object sender, DirectEventArgs e)
        {
            if (!string.IsNullOrEmpty(txtName.Text))
            {
                RichEditor1.DataType = txtDatatype.Text;
                PortalConfigBLL.SetValue(txtName.Text, RichEditor1.Value);
                {
                    (this.Page as Controls.BaseAdminPage).ShowNotification("Cập nhật thành công");
                    Window1.Hide();
                }
            }
        }

        protected void btnDelete_Click(object sender, DirectEventArgs e)
        {
            if (!string.IsNullOrEmpty(txtName.Text))
            {
                if (PortalConfigBLL.Delete(txtName.Text) > 0)
                {
                    (this.Page as Controls.BaseAdminPage).ShowNotification("Xóa thành công");
                    Window1.Hide();
                }
            }
        }
    }
}